package com.pipidan.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pipidan.blog.NotFoundException;
import com.pipidan.blog.dao.BlogDao;
import com.pipidan.blog.entity.Blog;
import com.pipidan.blog.mapper.BlogMapper;
import com.pipidan.blog.mapper.CommentMapper;
import com.pipidan.blog.mapper.MessageMapper;
import com.pipidan.blog.queryvo.*;
import com.pipidan.blog.util.MarkdownUtils;
import com.pipidan.blog.service.BlogService;
import javafx.util.StringConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.util.Date;
import java.util.List;

/**
 * @Description: 博客列表业务层接口实现类
 * @Date: Created in 23:47 2020/6/2
 * @Author: ONESTAR
 * @QQ群: 530311074
 * @URL: https://onestar.newstar.net.cn/
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogDao blogDao;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private MessageMapper messageMapper;
    //保存新增博客
    @Override
    public int saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        blog.setCommentCount(0);
        return blogMapper.insert(blog);
    }

    //查询文章管理列表
    @Override
    public List<BlogQuery> getAllBlog() {
        return blogMapper.getAllBlogQuery();
    }

    //删除博客
    @Override
    public void deleteBlog(Long id) {
        blogMapper.deleteById(id);
    }

    //查询编辑修改的文章
    @Override
    public ShowBlog getBlogById(Long id) {
        Blog blog = blogMapper.selectById(id);
        ShowBlog showBlog = new ShowBlog();
        BeanUtils.copyProperties(blog,showBlog);
        return  showBlog;
    }

    //编辑修改文章
    @Override
    public int updateBlog(ShowBlog showBlog) {
        showBlog.setUpdateTime(new Date());
        Blog blog = new Blog();
        BeanUtils.copyProperties(showBlog,blog);
        return blogMapper.insert(blog);
    }

    //搜索博客管理列表
    @Override
    public List<BlogQuery> getBlogBySearch(SearchBlog searchBlog) {
        return blogDao.searchByTitleAndType(searchBlog);
    }


    //查询首页最新博客列表信息
    @Override
//     @Cacheable(value = "blogList",key = "'blog'")       // redis缓存
    public List<FirstPageBlog> getAllFirstPageBlog() {
        return blogMapper.getFirstPageBlog();
    }

    //查询首页最新推荐信息
    @Override
    // @Cacheable(value = "commentblogList",key = "'commentblog'")       // redis缓存
    public List<RecommendBlog> getRecommendedBlog() {
        List<RecommendBlog> allRecommendBlog = blogMapper.getAllRecommendBlog();
        return allRecommendBlog;
    }

    //搜索博客列表
    @Override
    public List<FirstPageBlog> getSearchBlog(String query) {
        return blogMapper.getSearchBlog(query);
    }

    //统计博客总数
    @Override
    public Integer getBlogTotal() {
        return blogMapper.selectList(null).size();

    }

    //统计访问总数
    @Override
    public Integer getBlogViewTotal() {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.select("sum(views) as views");
        Blog blog = blogMapper.selectOne(wrapper);
        return blog==null? 0: blog.getViews();
    }

    //统计评论总数
    @Override
    public Integer getBlogCommentTotal() {
        return commentMapper.selectList(null).size();
    }

    //统计留言总数
    @Override
    public Integer getBlogMessageTotal() {
        return messageMapper.selectList(null).size();
    }

    //查询博客详情
    @Override
    public DetailedBlog getDetailedBlog(Long id) {
        DetailedBlog detailedBlog = blogMapper.getDetailedBlog(id);
        if (detailedBlog == null) {
            throw new NotFoundException("该博客不存在");
        }
        String content = detailedBlog.getContent();
        detailedBlog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        //文章访问数量自增
        blogMapper.updateViews(id);
        //文章评论数量更新
        blogMapper.getCommentCountById(id);
        return detailedBlog;
    }

    //分类页面查询
    @Override
    public List<FirstPageBlog> getByTypeId(Long typeId) {
        return blogDao.getByTypeId(typeId);
    }

    //查询最新评论
    @Override
    // @Cacheable(value = "NewCommentList",key = "'NewComment'")      // redis缓存
    public List<NewComment> getNewComment() {
        return blogMapper.getNewComment();
    }

}